package com.yx.front.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yx.front.config.global.CommonResult;
import com.yx.front.converter.DictMessageConverter;
import com.yx.front.enums.IntBoolean;
import com.yx.front.model.DictMessage;
import com.yx.front.model.vo.DictMessageVo;
import com.yx.front.service.DictMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author YangXiong
 */
@Component
public class DictManager {
    @Resource
    private DictMessageService dictMessageService;

    public CommonResult<List<DictMessageVo>> listByType(String type) {
        if (StringUtils.isEmpty(type)) {
            return CommonResult.success(null);
        }
        LambdaQueryWrapper<DictMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictMessage::getType, type);
        queryWrapper.eq(DictMessage::getIsChild, IntBoolean.YES.getKey());
        List<DictMessage> list = dictMessageService.list(queryWrapper);
        List<DictMessageVo> result = DictMessageConverter.handlerDictResult(list);
        return CommonResult.success(result);
    }

    public String getTextByValue(Integer value){
        LambdaQueryWrapper<DictMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictMessage::getValue, value)
                .eq(DictMessage::getType, "hobby");
        DictMessage one = dictMessageService.getOne(wrapper);
        if (Objects.isNull(one)){
            return "";
        }
        return one.getText();
    }

    public Integer getKeyByValue(String hobby) {
        LambdaQueryWrapper<DictMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictMessage::getText, hobby)
                .eq(DictMessage::getType, "hobby");
        DictMessage one = dictMessageService.getOne(wrapper);
        if (Objects.isNull(one)){
            return 0;
        }
        return one.getValue();
    }
}
